﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace UDS.Web.UI.Controls
{
    public class ClientSubmitButton : Button
    {
        /// <summary>
        /// Specify the id of the target control
        /// </summary>
        [PersistenceMode(PersistenceMode.Attribute)]
        public string ClientScript
        {
            get;
            set;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (Page != null)
            {
                if (!string.IsNullOrEmpty(ClientScript))
                {
                    StringBuilder sb = new StringBuilder(256);
                    sb.Append("try{");
                    sb.Append(ClientScript);
                    sb.Append("}catch(ex){alert('ERROR: '+ex.message);}return false;");

                    Page.ClientScript.RegisterOnSubmitStatement(GetType(), ClientID, sb.ToString());
                }
                else
                {
                    Page.ClientScript.RegisterOnSubmitStatement(GetType(), ClientID, "return false;");
                }
            }
        }
    }
}
